package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.domain.YdDepotHead;
import com.zhentao.domain.params.DepotHeadParams;
import com.zhentao.domain.YdDepotItem;
import com.zhentao.domain.dto.PurchaseOrderDto;
import com.zhentao.domain.vo.PurchaseOrderVO;
import com.zhentao.service.YdDepotHeadService;
import com.zhentao.mapper.YdDepotHeadMapper;
import com.zhentao.service.YdDepotItemService;
import com.zhentao.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @description 针对表【yd_depot_head(单据主表)】的数据库操作Service实现
*/
@Service
public class YdDepotHeadServiceImpl extends ServiceImpl<YdDepotHeadMapper, YdDepotHead>
    implements YdDepotHeadService{

    @Resource
    private YdDepotHeadMapper ydDepotHeadMapper;
    @Resource
    private YdDepotItemService ydDepotItemService;

    @Override
    public Result purchaseOrderList(PurchaseOrderDto dto) {

        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<YdDepotHead> list = ydDepotHeadMapper.purchaseOrderList(dto);
        PageInfo<YdDepotHead> pageInfo = new PageInfo<>(list);
        return Result.success().put("data",pageInfo.getList()).put("total",pageInfo.getTotal());
    }
    /*
    * 删除订单
    * */
    @Override
    public Boolean deleteOrder(int id) {
        Boolean flag = ydDepotHeadMapper.deleteOrder(id);
        return flag;
    }

    @Override
    public Result purchaseCGRK(PurchaseOrderDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<PurchaseOrderVO> list = ydDepotHeadMapper.purchaseCGRK(dto);
        PageInfo<PurchaseOrderVO> pageInfo = new PageInfo<>(list);
        return Result.success().put("data", pageInfo.getList()).put("total", pageInfo.getTotal());
    }
        public Result depotHeadList(DepotHeadParams params) {
//        LambdaQueryWrapper<YdDepotHead> wrapper=new LambdaQueryWrapper<>();
//        Page<YdDepotHead> page=new Page<>(params.getPageNum(), params.getPageSize());
//        page=ydDepotHeadMapper.selectPage(page,wrapper);
//        return Result.success(page);
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<YdDepotHead> ydDepotHeads=this.list();
        PageInfo<YdDepotHead> pageInfo=new PageInfo<>(ydDepotHeads);
        return Result.success(pageInfo);
    }

    @Override
    public Result addPurchaseCGDD(PurchaseOrderVO purchaseOrderVO) {
        YdDepotHead ydDepotHead = new YdDepotHead();
        YdDepotItem ydDepotItem = new YdDepotItem();
        BeanUtils.copyProperties(purchaseOrderVO,ydDepotHead);
        BeanUtils.copyProperties(purchaseOrderVO,ydDepotItem);
        ydDepotHead.setType("其他");
        ydDepotHead.setSubType("采购订单");
        ydDepotHead.setDefaultNumber(purchaseOrderVO.getNumber());
        ydDepotHead.setCreateTime(new Date());
        ydDepotHead.setOperTime(new Date());
        ydDepotHead.setCreator(65L);
        ydDepotHead.setStatus("0");
        this.save(ydDepotHead);

        ydDepotItem.setHeaderId(ydDepotHead.getId());

        ydDepotItemService.save(ydDepotItem);
        return Result.success();
    }}





